package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@author ${xinpl}
*@Description 菜单控制类
**/
@RestController
@RequestMapping("/Menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 查找所有菜单
     * @return
     */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        try {
            List<Menu>menuList=menuService.findAllMenu();
            return new Result(true,"成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改菜单失败!");
        }
    }
    /**
    *@author ${xinpl}
    *@Description 菜单数据分页
    *@Date 2020/4/16 11:43
    *@Param [queryPageBean]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.findPage(queryPageBean);
            List<Menu> parents = menuService.findParents();
            Map<String,Object> map = new HashMap<>();
            map.put("pageResult",pageResult);
            map.put("parents",parents);
            return new Result(true,"成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单分页失败!");
        }
    }

    /**
    *@author ${xinpl}
    *@Description 菜单新增
    *@Date 2020/4/16 14:44
    *@Param [menu]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,"新增菜单成功.");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单失败!");
        }

    }

    /**
    *@author ${xinpl}
    *@Description 菜单删除
    *@Date 2020/4/16 14:45
    *@Param [id]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/delete")
    public Result delete(int id,int level){
        try {
            menuService.delete(id,level);
            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"此菜单与用户关联,不能删除!!");
        }
    }

    /**
    *@author ${xinpl}
    *@Description 菜单修改
    *@Date 2020/4/16 14:45
    *@Param [menu]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改菜单失败!");
        }
    }


    /**
    *@author ${xinpl}
    *@Description 回显数据
    *@Date 2020/4/16 14:46
    *@Param [id]
    *@return com.itheima.constant.Result
    **/
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"数据加载成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单回显数据失败!");
        }
    }


}
